package com.example.administrator.managesystem.activity.firstpage.circle.ui;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.adapter.GridImageAdapter;
import com.example.administrator.managesystem.base.BaseActivity;
import com.example.administrator.managesystem.manager.FullyGridLayoutManager;
import com.example.administrator.managesystem.presenter.firstpage.FirstPagePresenter;
import com.example.administrator.managesystem.utils.ShareUtil;
import com.example.administrator.managesystem.utils.ToastUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;


public class SendCircleActivity extends BaseActivity implements CircleView{
    private LocationClient mLocationClient = null;    //LocationClient类是定位SDK的核心类

    private final static String TAG = SendCircleActivity.class.getSimpleName();
    private List<LocalMedia> selectList = new ArrayList<>();
    private List<String> list_seleted=new ArrayList<>();
    private RecyclerView recyclerView;
    private GridImageAdapter adapter;
    private int maxSelectNum = 9;
    private int themeId;
    private TextView location_tv,post_tv;
    private EditText post_content;


    @Override
    public void initView() {
        showfloatButton(true,true);
        themeId = R.style.picture_default_style;
        location_tv=findViewById(R.id.location_tv);
        post_content=findViewById(R.id.post_content);
        recyclerView =  findViewById(R.id.recycler);
        post_tv=findViewById(R.id.post_tv);
        FullyGridLayoutManager manager = new FullyGridLayoutManager(SendCircleActivity.this, 4, GridLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        adapter = new GridImageAdapter(SendCircleActivity.this, onAddPicClickListener);
        adapter.setList(selectList);
        adapter.setSelectMax(maxSelectNum);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                LocalMedia media = selectList.get(position);
                String pictureType = media.getPictureType();
                int mediaType = PictureMimeType.pictureToVideo(pictureType);
                switch (mediaType) {
                    case 1:
                        // 预览图片
                        PictureSelector.create(SendCircleActivity.this).externalPicturePreview(position, selectList);
                        break;
                    case 2:
                        // 预览视频
                        PictureSelector.create(SendCircleActivity.this).externalPictureVideo(media.getPath());
                        break;
                    case 3:
                        // 预览音频
                        PictureSelector.create(SendCircleActivity.this).externalPictureAudio(media.getPath());
                        break;
                }
            }
        });
    }

    @Override
    public void doBusiness() {
        post_tv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          list_seleted.clear();
          for(int i=0;i<selectList.size();i++){
              list_seleted.add(selectList.get(i).getCompressPath());
          }
          Log.e("msg","list_seleted:"+list_seleted);

          if(list_seleted.isEmpty()){
              ToastUtils.showToast(SendCircleActivity.this,"至少上传一张图片！");
              return;
          }
           showProgressDialog();

          if(location_tv.getText().toString().equalsIgnoreCase("是否开启定位？")){
              Log.e("msg","是否开启定位？");
              new CirclePresenter(SendCircleActivity.this).uploadLand(ShareUtil.getUserId(SendCircleActivity.this),
                      ShareUtil.getUserToken(SendCircleActivity.this),post_content.getText().toString(),"",list_seleted,SendCircleActivity.this);
          }else{
              new CirclePresenter(SendCircleActivity.this).uploadLand(ShareUtil.getUserId(SendCircleActivity.this),
                      ShareUtil.getUserToken(SendCircleActivity.this),post_content.getText().toString(),location_tv.getText().toString(),list_seleted,SendCircleActivity.this);
          }

      }
  });
    location_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //声明LocationClient类
                MyLocationListener myListener = new MyLocationListener();
                mLocationClient = new LocationClient(SendCircleActivity.this);
                //注册监听函数
                mLocationClient.registerLocationListener(myListener);
                LocationClientOption option = new LocationClientOption();
                option.setIsNeedAddress(true);//是否要地址
                option.setOpenGps(true);
                option.setCoorType("gcj02"); // 设置坐标类型
                option.setScanSpan(1000);//可选，默认0，即仅定位一次，设置发起连续定位请求的间隔需要大于等于1000ms才是有效的
                option.setOpenAutoNotifyMode();
                mLocationClient.setLocOption(option);
                mLocationClient.start();

            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_send_circle;
    }


    private GridImageAdapter.onAddPicClickListener onAddPicClickListener = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
                // 进入相册 以下是例子：不需要的api可以不写
                PictureSelector.create(SendCircleActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .imageFormat(PictureMimeType.PNG)
                        .previewImage(true)// 是否可预览图片
                        .theme(themeId)
                        .maxSelectNum(9)
                        .minSelectNum(1)
                        .selectionMode(PictureConfig.MULTIPLE)
                        .previewVideo(true)
                        .enablePreviewAudio(true) // 是否可播放音频
                        .isCamera(true)
                        .compress(true)
                        .glideOverride(160, 160)
                        .minimumCompressSize(100)// 小于 100kb 的图片不压缩
                        .cropCompressQuality(90)// 裁剪压缩质量 默认100
                        .selectionMedia(selectList)
                        .forResult(PictureConfig.CHOOSE_REQUEST);

        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择
                    selectList = PictureSelector.obtainMultipleResult(data);
                    adapter.setList(selectList);
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    }


    @Override
    public void navigateToHome() { finish(); }

    @Override
    public void showMsg() {
        dismissProgressDialog();
    }


    /***
     * 定位结果回调，在此方法中处理定位结果
     */
    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(final BDLocation location) {
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取地址相关的结果信息
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明
                     location_tv.setText(location.getProvince()+"-"+location.getCity()+"-"+location.getDistrict()+"-"+location.getStreet());


        }

    }


}
